package com.source.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Popedom;
import com.source.sys.entity.Role;
import com.source.sys.service.PopedomService;
import com.source.sys.service.RolePopedomService;
import com.source.sys.service.RoleService;
import com.source.sys.vo.PopedomVo;
import com.source.sys.vo.RolePopedomVo;
import com.source.sys.vo.RoleVo;
import com.source.utils.CommonResult;
import com.source.utils.ResultEnum;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Api(tags = "角色权限管理")
@RestController
@RequestMapping("/sys")
public class RolePopedomController {
    @Resource
    private RolePopedomService rolepopedomService;
    @Resource
    private RoleService roleService;
    @Resource
    private PopedomService popedomService;



    /**
     * //一级菜单，二级菜单，三级菜单
     *
     */
    @ApiOperation("获取权限的一级菜单")
    @GetMapping("/role-popedom/menu")
    public CommonResult listFirstLevelPopedomMenu(){
        List<Popedom> firstLevelPopedom = popedomService.getFirstLevelPopedom();
        if (!firstLevelPopedom.isEmpty()){
            return new CommonResult(ResultEnum.SUCCESS,firstLevelPopedom);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation("获取权限的子菜单")
    @GetMapping("/role-popedom/menu/{popedomId}")
    public CommonResult listSonPopedomMenu(@ApiParam("权限id") @PathVariable("popedomId") String popedomId){
        List<Popedom> sonPopedom = popedomService.getSonPopedom(popedomId);
        if (!sonPopedom.isEmpty()){
            return new CommonResult(ResultEnum.SUCCESS,sonPopedom);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }


    @ApiOperation(value = "添加角色权限")
    @PostMapping("/role-popedom")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "roleName",value = "角色名称",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "body",name = "popedomIdList",value = "权限ID列表",required = true)
    })
    public CommonResult addRolePopedom(@RequestParam(value = "roleName") String roleName,
                                       @RequestBody List<String> popedomIdList){
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("rname", roleName));
        if(role != null){
            return CommonResult.success("已有该角色名");
        }
        boolean addRolePopedom = rolepopedomService.addRolePopedom(roleName, popedomIdList);
        if (addRolePopedom == true){
            return new CommonResult(ResultEnum.SUCCESS);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "获取角色列表(分页)")
    @GetMapping("/role-popedom/{currentPageNo}/{size}")
    public CommonResult listRolePage(@ApiParam("当前页数")@PathVariable("currentPageNo") Integer currentPageNo,@ApiParam("每页显示条数")@PathVariable("size") Integer size){
        Page<RoleVo> rolePage = rolepopedomService.listRolePage(currentPageNo, size);
        if(!rolePage.getRecords().isEmpty()){
            //不为空
            return new CommonResult(ResultEnum.SUCCESS,rolePage);
        }else{
            return new CommonResult(ResultEnum.DATA_NOT_FOUND);
        }
    }

    @ApiOperation(value = "根据检索获取角色列表(分页)")
    @GetMapping("/role-popedom/{currentPageNo}/{size}/{queryKey}")
    public CommonResult listPopedomByRid(@ApiParam("当前页数")@PathVariable("currentPageNo") Integer currentPageNo,@ApiParam("每页显示条数")@PathVariable("size") Integer size,@ApiParam("查询关键词")@PathVariable("queryKey") String queryKey){
        Page<RoleVo> listPage = roleService.listRolePageByWrapper(queryKey, currentPageNo, size);
        if (!listPage.getRecords().isEmpty()){
            return new CommonResult(ResultEnum.SUCCESS,listPage);
        }else{
            return new CommonResult(ResultEnum.DATA_NOT_FOUND);
        }
    }

    @ApiOperation(value = "删除角色(逻辑删除)")
    @DeleteMapping("/role-popedom/{roleId}")
    public CommonResult deleteRole(@ApiParam("角色id")@PathVariable("roleId") String roleId){
        boolean deleteResult = roleService.deleteRoleById(roleId);
        if(deleteResult == true){
            return new CommonResult(ResultEnum.SUCCESS);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

    @ApiOperation(value = "根据角色id获取权限id和名称列表")
    @GetMapping("/role-popedom/{roleId}")
    public CommonResult listPopedomByRid(@ApiParam("角色id")@PathVariable("roleId") String roleId){
        List<PopedomVo> popedomList = rolepopedomService.listPopedomNameByRid(roleId);
        if(!popedomList.isEmpty()){
            return new CommonResult(ResultEnum.SUCCESS,popedomList);
        }else{
            return new CommonResult(ResultEnum.DATA_NOT_FOUND.getCode(),"权限为空");
        }
    }



    /**
     *
     * @param roleId
     * @return
     */
    @ApiOperation(value = "回显该角色的角色和权限信息")
    @GetMapping("/role-popedom/info/{roleId}")
    public CommonResult showRolePopedomById(@ApiParam("角色id")@PathVariable("roleId") String roleId){
        RolePopedomVo rolePopedomVo = rolepopedomService.showRolePopedomInfoByRoleId(roleId);
        if (rolePopedomVo != null){
            return new CommonResult(ResultEnum.SUCCESS,rolePopedomVo);
        }else{
            return new CommonResult(ResultEnum.ERROR.getCode(),"无此角色id");
        }
    }


    @ApiOperation(value = "角色权限修改")
    @PutMapping("/role-popedom")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "roleId",value = "角色id",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "query",name = "roleName",value = "角色名称",required = true,dataType = "String"),
            @ApiImplicitParam(paramType = "body",name = "popedomIdList",value = "权限ID列表",required = true)
    })
    public CommonResult updateRolePopedomByRoleId(String roleId,
                                                  String roleName,
                                                  @RequestBody List<String> popedomList){
        boolean updateResult = rolepopedomService.updateRolePopedomByRoleId(roleId, roleName, popedomList);
        if (updateResult == true) {
            return new CommonResult(ResultEnum.SUCCESS);
        }else{
            return new CommonResult(ResultEnum.ERROR);
        }
    }

}

